/* =============================================================
 * common-method.js v1.0.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 14/01/2020
 * ============================================================ */

// 前面加分号是为了防止前面的代码没有写分号结尾影响程序。这里采用自执行函数，防止全局被污染
;(function ($) {
    // $.extend等于$.fn.extend等于jQuery.prototype.extend。该方法会把后面的参数覆盖前面的参数，相同参数替换，不存在的参数则添加。
    $.extend($.fn, {

        // 将  "{min=0,aver=0.10,max=0.60}"  转为对象
        strToObject: function (data) {
            let obj = {};
            str = str.replace("{", "").replace("}", "");
            str.replace(/(\w+)(?:=([^,]*))?/g, function (str, key, value) {
                obj[key] = value;
            });
            return obj;
        },

        // 字符串转数字
        strToNumber: function (str, redix) {
            redix = redix ? redix : 10;// 如果是整数  默认10进制转换
            if (/^-?\d+$/.test(str)) {// 整数
                return parseInt(str, redix);
            } else if (/^(-?\d+)(\.\d+)?$/.test(str)) {// 浮点数
                return parseFloat(str);
            }
            return str;
        },

        // 对一个字符串或数字取整数或小数部分
        getNumberPart: function (param, type) {
            let num = param instanceof String ? this.strToObject(param) : param;
            if (!$.isNumeric(num)) return param;

            if (type === 10) {// 取整数部分
                return Math.trunc(num);
            } else if (type === 20) {// 取小数部分
                return this.subtraction(num, Math.trunc(num));
            }
            return param;
        },

        // 加法
        addition: function (a, b) {
            let c, d, e;
            try {
                a == null ? c = "0" : c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                b == null ? d = "0" : d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), (this.multiplication(a, e) + this.multiplication(b, e)) / e;
        },

        // 减法
        subtraction: function (a, b) {
            let c, d, e;
            try {
                a == null ? c = "0" : c = a.toString().split(".")[1].length;
            } catch (f) {
                c = 0;
            }
            try {
                b == null ? d = "0" : d = b.toString().split(".")[1].length;
            } catch (f) {
                d = 0;
            }
            return e = Math.pow(10, Math.max(c, d)), (this.multiplication(a, e) - this.multiplication(b, e)) / e;
        },

        // 乘法
        multiplication: function (a, b) {
            let c = 0, d = a == null ? "0" : a.toString(), e = b == null ? "0" : b.toString();

            try {
                c += d.split(".")[1].length;
            } catch (f) {
            }
            try {
                c += e.split(".")[1].length;
            } catch (f) {
            }

            return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
        },

        // 除法
        division: function (a, b) {
            let c, d, e = 0, f = 0;

            try {
                a == null ? e = "0" : e = a.toString().split(".")[1].length;
            } catch (g) {
            }
            try {
                b == null ? f = "0" : f = b.toString().split(".")[1].length;
            } catch (g) {
            }

            return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), this.multiplication(c / d, Math.pow(10, f - e));
        },
    })
})(Zepto);

